/*
 * This file contains classes and functions that are used all over the 
 * application.
 * 
 * Must be included after:
 *  - basic_util.js
 * 
 */

// Reference for the canvas to draw onto.
var canvas;
// Reference to the drawing context of the canvas.
var canvasCtx;
//var canvasDiv;

var diagramOptionsDiv;
var diagramHeaderDiv;
var projectExplorerTreeDiv;

var project;

window.onload = function() {

    canvas = document.getElementById("mainCanvas");
    canvasCtx = canvas.getContext("2d");

    diagramOptionsDiv = document.getElementById("options");
    diagramHeaderDiv = document.getElementById("diagramTabs");
    projectExplorerTreeDiv = document.getElementById("projecttree");
    editorDiv = document.getElementById("editor");
    
    project = new Project("p");
    project.evNewProject();
    
    // CreateTestProject();
}

function CreateTestProject() {
    var class0 = new Class("EventResult");
    var class1 = new Class("Event");

    var testCallback = {
        handleEvent: function(evt) {
            test.innerHTML += "<br> event callback! [";
            for(a in evt) {
                test.innerHTML += a + "='" + evt[a] + "' , ";
            }
            test.innerHTML += "]";
        }
    };
    
    var cl1 = new Class("EventDispatcher");

    var st0 = new Stereotype("inout");
    var st1 = new Stereotype("singleton");
    var st2 = new Stereotype("boundary");
    var st3 = new Stereotype("power_overwhelming");

    var attr0 = new Attribute("eventCounter");
    attr0.setType(PrimitiveType.integer);
    attr0.setVisibility("-");
    
    var attr1 = new Attribute("defaultDispatcher");
    attr1.setType(cl1);
    attr1.setVisibility("-");
    attr1.setDefaultValue("new EventDispatcher()");
    attr1.setIsClassifierScope(true);

    var op0 = new Operation("dispatchEvents");
    op0.setIsAbstract(true);
    op0.setReturnType(class0);

    var par00 = new Parameter("evt");
    par00.setType(class1);
    par00.addStereotype(st0);

    var par01 = new Parameter("maxDepth");
    par01.setType(PrimitiveType.integer);
    par01.setDefaultValue(2);

    var op1 = new Operation("setDefaultDispatcher");
    op1.setVisibility("+");
    op1.setIsClassifierScope(true);

    var par10 = new Parameter("disp");
    par10.setType(cl1);

    var op2 = new Operation("getDefaultDispatcher");
    op2.setReturnType(cl1);
    op2.setIsClassifierScope(true);
    op2.addStereotype(st1);

    var op3 = new Operation("getEventCounter");
    op3.setReturnType(PrimitiveType.integer);

    op0.addParameter(par00);
    op0.addParameter(par01);
    
    op1.addParameter(par10);
    
    cl1.addAttribute(attr0);
    cl1.addAttribute(attr1);
    cl1.addOperation(op2);
//    cl1.addOperation(op1);
    cl1.addOperation(op0);
    cl1.addOperation(op3);
//    cl.addStereotype(st2);
    
    cl1.setIsAbstract(true);
    
    var cl2 = new Class("SwarmInterface", true);
    var op4 = new Operation("doSwarm");
    op4.setReturnType(PrimitiveType.boolean);
    cl2.addOperation(op4);
//    cl2.addStereotype(st2);
    
    var cl4 = new Class("ProtossInterceptor");
    var attr3 = new Attribute("hp");
    attr3.setType(PrimitiveType.integer);
    attr3.setDefaultValue("100");
    attr3.setVisibility("-");
    cl4.addAttribute(attr3);
    
    var cl3 = new Class("CarrierHasArrived");
    var op5 = new Operation("releaseInterceptors");
    var op6 = new Operation("focusTassadarsEnergies");
    var par60 = new Parameter("password");
    par60.setType(PrimitiveType.string);
    op6.addParameter(par60);
    op6.setVisibility("#");
    var attr2 = new Attribute("interceptors");
    attr2.setType(cl4);
    attr2.setVisibility("-");
    attr2.setMultiplicity("8");
    cl3.addAttribute(attr2);
    cl3.addOperation(op6);
    cl3.addOperation(op5);
    cl3.addStereotype(st3);
    
    var rel0 = new Association("association",true, false,false);
    var rel1 = new Association("aggregation",false,true, false);
    var rel2 = new Association("composition",false,false,true);
    var rel3 = new Inheritance("generalization",true,false);
    var rel4 = new Inheritance("realization",false,true);
    var rel5 = new Dependency("dependency");
    
    rel0.targetEndInfo.name = "motherShip";
    rel0.targetEndInfo.multiplicity = 1;
    rel0.targetEndInfo.visibility = "-";
    rel0.sourceEndInfo.isNavigable = false;
    
    rel0.setSourceEnd(cl4);
    rel0.setTargetEnd(cl3);
    rel1.setSourceEnd(cl1);
    rel1.setTargetEnd(cl2);
    rel2.setSourceEnd(cl1);
    rel2.setTargetEnd(cl4);
    rel3.setSourceEnd(cl1);
    rel3.setTargetEnd(cl3);
    rel4.setSourceEnd(cl3);
    rel4.setTargetEnd(cl2);
    rel5.setSourceEnd(cl2);
    rel5.setTargetEnd(cl3);
    
    rel3.addStereotype(st2);
    
    var cl1graph = new ClassGraphics(new Position(350,400),cl1);
    var cl2graph = new ClassGraphics(new Position(10,420), cl2);
    var cl3graph = new ClassGraphics(new Position(30,150), cl3);
    var cl4graph = new ClassGraphics(new Position(500,100), cl4);
    var conn0 = new RelationshipGraphics(rel0, cl4graph, cl3graph);
    var conn1 = new RelationshipGraphics(rel1, cl1graph, cl2graph);
    var conn2 = new RelationshipGraphics(rel2, cl1graph, cl4graph);
    var conn3 = new RelationshipGraphics(rel3, cl1graph, cl3graph);
    var conn4 = new RelationshipGraphics(rel4, cl3graph, cl2graph);
    var conn5 = new RelationshipGraphics(rel5, cl2graph, cl3graph);
    
    var diagram1 = new Diagram("Diagram_2");
    diagram1.addElement(cl1graph);
    diagram1.addElement(cl2graph);
    diagram1.addElement(cl3graph);
    diagram1.addElement(cl4graph);
    diagram1.addElement(conn0);
    diagram1.addElement(conn1);
    diagram1.addElement(conn2);
    diagram1.addElement(conn3);
    diagram1.addElement(conn4);
    diagram1.addElement(conn5);
    
    var cl1graph2 = new ClassGraphics(new Position(350,400),cl1);
//    var cl2graph2 = new ClassGraphics(new Position(10,420), cl2);
    var cl3graph2 = new ClassGraphics(new Position(30,150), cl3);
    var cl4graph2 = new ClassGraphics(new Position(450,30), cl4);
    var conn02 = new RelationshipGraphics(rel0, cl4graph2, cl3graph2);
//    var conn12 = new RelationshipGraphics(rel1, cl1graph2, cl2graph2);
    var conn22 = new RelationshipGraphics(rel2, cl1graph2, cl4graph2);
    var conn32 = new RelationshipGraphics(rel3, cl1graph2, cl3graph2);
//    var conn42 = new RelationshipGraphics(rel4, cl3graph2, cl2graph2);
//    var conn52 = new RelationshipGraphics(rel5, cl2graph2, cl3graph2);
    
    var cl5graph2 = new ClassGraphics(new Position(610,150), class0);
    var cl6graph2 = new ClassGraphics(new Position(610,250), class1);
    
    var diagram2 = new Diagram("LongerNamedDiagram");
    diagram2.addElement(cl1graph2);
//    diagram2.addElement(cl2graph2);
    diagram2.addElement(cl3graph2);
    diagram2.addElement(cl4graph2);
    diagram2.addElement(conn02);
//    diagram2.addElement(conn12);
    diagram2.addElement(conn22);
    diagram2.addElement(conn32);
//    diagram2.addElement(conn42);
//    diagram2.addElement(conn52);

    diagram2.addElement(cl5graph2);
    diagram2.addElement(cl6graph2);
    
    cl1graph.name = "EventDispatcherGraph_1";
    cl2graph.name = "SwarmInterfaceGraph_1";
    cl3graph.name = "CarrierHasArrivedGraph_1";
    cl4graph.name = "ProtossInterceptorGraph_1";
    
    conn0.name = "Conn0_1";
    conn1.name = "Conn1_1";
    conn2.name = "Conn2_1";
    conn3.name = "Conn3_1";
    conn4.name = "Conn4_1";
    conn5.name = "Conn5_1";
    
    cl1graph2.name = "EventDispatcherGraph_2";
//    cl2graph2.name = "SwarmInterfaceGraph_2";
    cl3graph2.name = "CarrierHasArrivedGraph_2";
    cl4graph2.name = "ProtossInterceptorGraph_2";
    cl5graph2.name = "EventResult_2";
    cl6graph2.name = "Event_2";
    
    conn02.name = "Conn0_2";
//    conn12.name = "Conn1_2";
    conn22.name = "Conn2_2";
    conn32.name = "Conn3_2";
//    conn42.name = "Conn4_2";
//    conn52.name = "Conn5_2";
    
    model = new Model();
    model.addElement(cl1);
    model.addElement(cl2);
    model.addElement(cl3);
    model.addElement(cl4);
    model.addElement(rel0);
    model.addElement(rel1);
    model.addElement(rel2);
    model.addElement(rel3);
    model.addElement(rel4);
    model.addElement(rel5);
    model.addElement(st0);
    model.addElement(st1);
    model.addElement(st2);
    model.addElement(st3);
    
    model.addElement(class0);
    model.addElement(class1);
    
//    for(id in model.modelElements) {
//        diagramOptionsDiv.innerHTML += "<input type='button' onclick='model.removeAndDestroyElement(model.getElementById("+id+"))' value='Destroy "+model.getElementById(id).name+" (uid="+id+")'/><br/>";
//    }
    
    project = new Project("UrukHaiSampleProject");
    project.setModel(model);
    project.addDiagram(diagram1);
    project.addDiagram(diagram2);
    project.switchToDiagram(diagram1);
    
    document.getElementById("currentproject")
            .innerHTML = "Project: " + project.name;
    
};
